set scheme stcolor


use "../Metadata/tess_analysisdata.dta", clear

* drop no difference tests & tests without critical N estimate
keep if insample==1

sum N_person, d 
scalar medianhyp=r(p50)

* critical N is <= median N of hypotheses
sum crit_N
gen critN_undermedhyp=.
replace critN_undermedhyp=1 if crit_N_bonf <= medianhyp
replace critN_undermedhyp=0 if crit_N_bonf > medianhyp 

replace critN_undermedhyp=0 if rightdir=="no"
tab critN_undermedhyp


di medianhyp


tab Npersoncat, sum(critN_undermedhyp)



* create frame
cap frame change default
cap frame drop results
frame create results strL varname mean se ul ll str8 N



* critical N <median estimation N

	proportion critN_undermedhyp if N_person<=1000, level(83.4) vce(cluster vendor_id)
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	sum critN_undermedhyp if N_person<=1000 
	local N= `r(N)'		
	frame post results ("1000 or less") (`mean') (`se') (`ll') (`ul') ("N=`N'")
		
	proportion critN_undermedhyp if N_person>=1001 & N_person<=2000, level(83.4) vce(cluster vendor_id)		
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	sum critN_undermedhyp if N_person>=1001 & N_person<=2000 
	local N= `r(N)'		
	frame post results ("1001-2000") (`mean') (`se') (`ll') (`ul') ("N=`N'")

	proportion critN_undermedhyp if N_person>=2001 & N_person<=3000, level(83.4) vce(cluster vendor_id)		
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	sum critN_undermedhyp if N_person>=2001 & N_person<=3000
	local N= `r(N)'		
	frame post results ("2001-3000") (`mean') (`se') (`ll') (`ul') ("N=`N'")
	
	proportion critN_undermedhyp if N_person>=3001, level(83.4) vce(cluster vendor_id)	
	mat table = r(table)	
	local mean = table[1,2]
	local se = table[2,2]
	local ll = table[5,2]
	local ul = table[6,2]
	sum critN_undermedhyp if N_person>=3001
	local N= `r(N)'		
	frame post results ("Over 3001") (`mean') (`se') (`ll') (`ul') ("N=`N'")	
		


* graph

sum N_person, d
local medN=r(p50)
di `medN'

frame change results
gen xlabel=_n
labmask xlabel, values(varname)
gen mlabel=0.1



* crit N< threshold
		graph twoway ///
		(bar mean xlabel if xlabel<=4, barw(0.8) fc(stc1%50) colordiscrete lc(none)) || ///
		(rcap ll ul xlabel if xlabel<=4,lc(gs7)) ///
		(scatter mlabel xlabel if xlabel<=4, ///		
msym(none) mlab(N) mlabpos(6) mlabcolor(black) mlabsize(3.4) mlabangle(hor)) ///
		, ///	
			ytitle("Pr(Critical N< Median N)", size(3.8) margin(r=4 l=-2)) ///
			xlabel( ///
			, labsize(3) angle(hor) valuelabels) ///
			xtitle("Test sample size", margin(b=2 t=2) size(3.8)) ///
			ylabel(0(0.2)1, labsize(3.8) nogrid) ///
			scheme(s2mono) ///
			graphregion(color(white)) ///
			legend(off) name(hyp, replace) 
	

	frame change default
			
graph export "figures/Figure3-CriticalN.pdf", replace
graph export "figures/Figure3-CriticalN.png", replace	
